import { AuditTConst } from "src/consts/audit_template_const";
import { PopupName } from "src/consts/popup_const";
import * as Api from 'src/api/net';

const state: any = {} as any;
// StateIniter.init(state, 'demo');

state.nodes = [];
state.bind_business = []
state.id = ""
state.name = ""
state.status = ""
state.bind_status = ""
state.remark = ""
state.version = ""
state.feature_code = ""

// vuex

/** 更新数据的 */
const mutations = {
    [AuditTConst.CommitAuditTShowList](state: any, data: any): void {
        console.log("Member_Panel data update", data);
        if (isNoEmptyObject(data)) {
            state.id = data.id
            state.name = data.name
            state.status = data.status
            state.bind_status = data.bind_status
            state.remark = data.remark
            state.feature_code = data.feature_code
            if (data.bind_business) {
                state.bind_business = data.bind_business
            }
            state.nodes = data.nodes
            state.version = data.version
        } else {
            state.id = ""
            state.name = ""
            state.status = ""
            state.bind_status = ""
            state.remark = ""
            state.feature_code = ""
            state.bind_business = []
            state.nodes = []
            state.version = ""
        }
        if (WLStore().state.auditTemplate.type == "edit") {
            WLLib.panel(PopupName.AuditT_Panel);
        }
    },
    [AuditTConst.CommitBusiness](state: any, data: any): void {
        state.bind_business = data
        WLLib.panel(PopupName.AuditT_Business_Panel).close();
    },
    [AuditTConst.CommitRole](state: any, data: any): void {
        if (WLStore().state.auditTRole.type == "role_edit") {
            state.nodes.splice(data.ind, 1, data)
        } else if (WLStore().state.auditTRole.type == "role_add") {
            state.nodes.push(data)
        }

        WLLib.panel(PopupName.AuditT_Role).close()
    }
}

//异步处理
const actions = {
    [AuditTConst.DispatchAuditTShow](context: VuexData, data?: any): void {
        Api.AjaxPost(Config.ajaxUrl + "/audit/show.do", data, (res: any) => {
            if (isNoEmptyObject(res.data)) {
                WLCommit(AuditTConst.CommitAuditTShowList, res.data);
            }
        })
    },
    [AuditTConst.DispatchAuditTEdit](context: VuexData, data?: any): void {
        Api.AjaxPost(Config.ajaxUrl + "/audit/edit.do", data, (res: any) => {
            WLMessage("编辑权限成功", "success")
            let senData: any = {}
            senData["curr_page"] = WLStore().state.auditTemplate.curr_page
            senData["page_size"] = WLStore().state.auditTemplate.page_size
            WLDispatch(AuditTConst.DispatchAuditTList, senData);
            WLLib.panel(PopupName.AuditT_Panel).close();
        })
    },
    [AuditTConst.DispatchAuditTAdd](context: VuexData, data?: any): void {
        Api.AjaxPost(Config.ajaxUrl + "/audit/add.do", data, (res: any) => {
            WLMessage("新增角色成功", "success")
            let senData: any = {}
            senData["curr_page"] = WLStore().state.auditTemplate.curr_page
            senData["page_size"] = WLStore().state.auditTemplate.page_size
            WLDispatch(AuditTConst.DispatchAuditTList, senData);
            WLLib.panel(PopupName.AuditT_Panel).close();
        })
    }
}

export default {
    state,
    mutations,
    actions
}